<?php

class Ead_MinibannerController extends App_Controller_Action
{
    public $dir = 'public/upload/minibanner/';

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $obj = new Ead_Model_DbTable_Minibanner();
        $result = $obj->fetchAll($obj->select()->order(array('BAN_ORD ASC')));
        $this->view->result = $this->paginacao($result);
    }
    
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ckeditor/ckeditor.js');
        
        $form = new Ead_Form_Minibanner();
        $form->submit->setLabel('Gravar');
        
        $option = array('width'=>'70%');
        echo $this->view->CkeditorHelper('BAN_RESUMO',$option);
        echo $this->view->CkeditorHelper('BAN_TEXTO',$option);
        
        Zend_Loader::loadClass('imagem','library');
        
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('BAN_ID');
                
                $returnImg = imagem::upload('BAN_FILE',$this->dir,$id);
                
                $data = array(
                    'BAN_ID'     => $form->getValue('BAN_ID'),
                    'BAN_DSC'    => $form->getValue('BAN_DSC'),
                    'BAN_LNK'    => $form->getValue('BAN_LNK'),
                    'BAN_STA'    => $form->getValue('BAN_STA'),
                    'BAN_ORD'    => $form->getValue('BAN_ORD'),
                );
                
                $obj = new Ead_Model_DbTable_Minibanner();
                
                if ($id == 0){
                    $obj->insert($data);  
                    $id =  $obj->getAdapter()->lastInsertId();                    
                    imagem::rename($id,$this->dir,$returnImg['filenew']);
                }
                else{
                    $obj->update($data, 'BAN_ID = ' . $id);
                }
                
                if(strlen($_FILES['BAN_FILE']['tmp_name']) > 1){
                    $img = glob($this->dir.$id.'.*');
                    foreach($img as $filetemp) $filename = $filetemp;
                    imagem::reizeFull($filename,$filename, '390', '200');
                } 
                
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
            
            
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Minibanner();
                $form->populate($obj->fetchRow('BAN_ID = ' . $id)->toArray());
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Ead_Model_DbTable_Minibanner();
            $ok = (bool) $obj->delete('BAN_ID = '.(int)$id);
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir);
            //$this->_helper->redirector('index'); 
        } else {
            //$id = $this->_getParam('id', 0);
            //$obj = new Ead_Model_DbTable_Minibanner();
            //$form->populate($obj->fetchRow('BAN_ID = ' . $id)->toArray());
        }
        
        echo json_encode(array('ok' => $ok));
    }

}

